package com.furtherstar.auth.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.furtherstar.auth.controller.dto.AppQuery;
import com.furtherstar.auth.controller.dto.AppUserQuery;
import com.furtherstar.auth.dao.model.App;
import com.furtherstar.auth.service.AppService;
import com.furtherstar.auth.service.UserRoleService;
import com.github.pagehelper.PageSerializable;
import com.yummy.convention.exception.AssertUtil;
import com.yummy.convention.http.Response;
import com.yummy.web.security.bean.SimpleUser;

@RestController
@RequestMapping("/app")
public class AppController {
	
	@Autowired
	private AppService appService;
	@Autowired
	private UserRoleService userRoleService;

	@PostMapping("/add")
	public Response<Long> add(@RequestBody App app){
		AssertUtil.isTrue(StringUtils.isNotBlank(app.getAppname()),"应用名称不能为空");
		AssertUtil.isTrue(StringUtils.isNotBlank(app.getAppTitle()),"应用昵称不能为空");
		return Response.success(appService.add(app));
	}

	@PostMapping("/update")
	public Response<Long> update(@RequestBody App app){
		AssertUtil.notNull(app.getId(),"id不能为空");
		return Response.success(appService.update(app));
	}

	@PostMapping("/disable")
	public Response<Long> disable(@RequestBody App app){
		AssertUtil.notNull(app.getId(),"id不能为空");
		return Response.success(appService.disable(app.getId()));
	}

	@PostMapping("/enable")
	public Response<Long> enable(@RequestBody App app){
		AssertUtil.notNull(app.getId(),"id不能为空");
		return Response.success(appService.enable(app.getId()));
	}

	@PostMapping("/delete")
	public Response<Boolean> delete(@RequestBody App app){
		AssertUtil.notNull(app.getId(),"id不能为空");
		return Response.success(appService.delete(app.getId()));
	}

	@PostMapping("/page")
	public Response<PageSerializable<App>> queryPage(@RequestBody AppQuery appQuery){
		return Response.success(appService.queryPage(appQuery));
	}

	@PostMapping("/list")
	public Response<List<App>> queryList(@RequestBody AppQuery appQuery){
		return Response.success(appService.queryList(appQuery));
	}

	@PostMapping("/user/page")
	public Response<PageSerializable<SimpleUser>> queryPage(@RequestBody AppUserQuery appUserQuery){
		return Response.success(userRoleService.queryPage(appUserQuery));
	}
	
}
